package org.wang.chuangen.session.handler;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
import org.wang.chuangen.session.dao.RedisDao;
import org.wang.chuangen.session.db.RedisConn;
import org.wang.chuangen.session.util.Tool;

public class RedisSessionHandlerValve extends ValveBase{
	
	private RedisDao redisDao=new RedisDao(RedisConn.getInstance());

	@Override
	public void invoke(Request request, Response response) throws IOException,	ServletException {
		try {
			getNext().invoke(request, response);
		} finally {
			Session session=request.getSessionInternal(false);
			if(session!=null){
				Tool.fine("在请求结束时存储session数据,sessionId:"+session.getId());
				redisDao.saveSession(session);
			}
		}
	}

}
